
package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;
import com.duobao.wugong.WugongManager;

import java.awt.event.*;

public class Yandu extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    Equip equip;
    JLabel fanhui;
    JLabel fanhuiyouxi;
    WuGong wugon;

    JLabel tishi;
    JLabel shenjitishi;
    JLabel jishentishi;
    public Yandu(DuobaoSwingLaunch game, Equip equip)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);
        this.equip = equip;


        xiaoguo();

        setFanhui();
        setFanhuiyouxi();
    }
    private void xiaoguo()
    {
        String name=this.equip.getWugongName();//获得书籍上记载的武功名称
        WuGong wug=this.game.hero.getWg(name);//获取自己的武功


        if(wug==null)
        {

            try {
                this.wugon=WugongManager.getEques(name).newInstance(1);
                this.game.hero.getWGs().add(this.wugon);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            setShenjitishi();
        }else
        {
            this.wugon=wug;
            float f=((float)this.game.hero.getSP())/((float)this.game.hero.getMaxSP());
            if(f<=0.1f)
            {
                setJishentishi();
            }else
            {
                if(this.game.hero.xueyi_shuIf(this.wugon))
                {
                    this.game.hero.xueyiShu(this.wugon);
                    setShenjitishi();
                }else
                {
                    this.game.hero.xueyiShu(this.wugon);
                    setTishi();
                }

            }
        }
    }
    private void setTishi() {
        tishi=new JLabel("你照着书上的内容比划了起来，视乎有些进展("+this.wugon.getLv()+"级/"+this.wugon.getEndurance()+"%)");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setShenjitishi() {
         String result = Zuanhuan.zhuang(this.wugon.getLv());

        tishi=new JLabel(this.wugon.getName()+"等级提高到"+result+"级");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setJishentishi() {
        tishi=new JLabel("你看起来昏昏欲睡，快去休息吧");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }

    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==tishi)
        {
            f=true;
        }
        return f;
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            try {
                this.game.setPanel(new Wuping(this.game,this.equip));
            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
